package com.apanal.qlife.common.dictionary;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.factory.SpringContextHolder;
import com.apanal.qlife.sys.dao.DictionaryMapper;
import com.apanal.qlife.sys.model.Dictionary;

/**
 * 数据字典类
 * 
 * @author shuliangxing
 * 
 * @date 2015-1-14上午11:14:55
 */
public class DictionaryMap {

	/**
	 * 中文lis
	 */
	private Map<String, List<Dictionary>> zhCnMap;

	/**
	 * 英文map
	 */
	private Map<String, List<Dictionary>> enUsMap;

	private DictionaryMap() {
		init();
	}

	/**
	 * 初始化map
	 */
	private void init() {
		// 实例化map
		zhCnMap = new HashMap<String, List<Dictionary>>();
		enUsMap = new HashMap<String, List<Dictionary>>();

		// 获取ioc容器管理的对象
		DictionaryMapper dictionaryMapper = SpringContextHolder
				.getBean("dictionaryMapper");
		List<String> types = dictionaryMapper.selectAllType();
		for (String type : types) {
			Dictionary dictionary = new Dictionary();
			dictionary.setType(type);
			List<Dictionary> dictionaries = dictionaryMapper
					.selectAllByType(dictionary);
			List<Dictionary> cList = new ArrayList<Dictionary>();
			List<Dictionary> uList = new ArrayList<Dictionary>();
			for (Dictionary dic : dictionaries) {
				if (Constants.LANGUAGE_EN_US.equals(dic.getLanguage())) {
					uList.add(dic);
				} else {
					cList.add(dic);
				}
			}
			zhCnMap.put(type, cList);
			enUsMap.put(type, uList);
		}
	}

	/**
	 * 静态内部类实现单例模式
	 * 
	 * 
	 * @author shuliangxing
	 * 
	 * @date 2015-3-16下午2:23:17
	 */
	private static class DictionaryMapFactory {
		private static final DictionaryMap instance = new DictionaryMap();
	}

	/**
	 * 根据类型和语言获取字典集合
	 * 
	 * @param type
	 * @param lang
	 * @return
	 */
	public static List<Dictionary> getList(String type, String lang) {
		if (Constants.LANGUAGE_EN_US.equals(lang)) {
			return DictionaryMapFactory.instance.enUsMap.get(type);
		} else {
			return DictionaryMapFactory.instance.zhCnMap.get(type);
		}
	}

	/**
	 * 根据类型获取中文字典集合
	 * 
	 * @param type
	 * @param lang
	 * @return
	 */
	public static List<Dictionary> getList(String type) {
		return getList(type, Constants.LANGUAGE_ZH_CN);
	}

	/**
	 * 根据类型语言获取字典map
	 * 
	 * @param type
	 * @param lang
	 * @return
	 */
	public static Map<Integer, String> getMap(String type, String lang) {
		Map<Integer, String> map = new HashMap<Integer, String>();
		List<Dictionary> list = getList(type, lang);
		if (list != null && !list.isEmpty()) {
			for (Dictionary dic : list) {
				map.put(dic.getCode(), dic.getContent());
			}
		}
		return map;

	}

	/**
	 * 根据类型获取中文字典map
	 * 
	 * @param type
	 * @return
	 */
	public static Map<Integer, String> getMap(String type) {
		return getMap(type, Constants.LANGUAGE_ZH_CN);
	}

	/**
	 * 根据类型获取中文字典反转map,即原有map的"值""键"掉换顺序
	 * 
	 * @param type
	 * @return
	 */
	public static Map<String, Integer> getReversedMap(String type) {
		Map<String, Integer> rMap = new HashMap<String, Integer>();
		Map<Integer, String> map = getMap(type);
		if (map != null) {
			Iterator<Integer> iterator = map.keySet().iterator();
			while (iterator.hasNext()) {
				Integer key = (Integer) iterator.next();
				rMap.put(map.get(key), key);
			}
		}
		return rMap;
	}

	/**
	 * 根据类型和键获取中文字典map对应的值
	 * 
	 * @param type
	 * @param key
	 * @return
	 */
	public static String getValue(String type, Integer key) {
		String value = getMap(type).get(key);
		return value == null ? String.valueOf(key) : value;
	}

	/**
	 * 重新刷新字典缓存map
	 */
	public static void reload() {
		DictionaryMapFactory.instance.init();
	}

}
